<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h2 {
            text-align: center;
        }

        .box {
            width: 600px;
            margin: 50px auto;
            display: flex;
            font-size: 25px;
            line-height: 40px;
        }

        .qs {

            width: 450px;
            height: 40px;
            color: red;

        }

        .btns {
            text-align: center;
        }

        .btns button {
            width: 120px;
            height: 35px;
            margin: 0 50px;
        }
    </style>
</head>

<body>
<h2>随机点名</h2>
<div class="box">
    <span>名字是：</span>
    <div class="qs">这里显示姓名</div>
</div>
<div class="btns">
    <button class="start">开始</button>
    <button class="end">结束</button>
</div>

<script>
    let names = ['段俊杰', '郑子恒', '秦琪', '楚浩天', '归子豪'];
    //定时器的全局变量
    let timer = 0;
    //随机数的变量
    let random = 0;
    //获取数据
    const start = document.querySelector('.start');
    const name = document.querySelector('.qs');
    //添加开始抽奖点击事件
    start.addEventListener('click', function () {
        //开启定时器
        timer = setInterval(function () {
            //将数组中的姓名随机生成到页面中
            random = Math.floor(Math.random() * names.length)
            //获取姓名的数据
            name.innerText = names[random]
        }, 50)
        if (names.length === 1) {
            start.disabled = true
            end.disabled = true
        }
    })
    //添加停止抽奖的点击事件
    const end = document.querySelector('.end');
    end.addEventListener('click', function () {
        //关闭定时器
        clearInterval(timer);
        //删除当前数组中的名字
        names.splice(random, 1)
    })
</script>
</body>

</html>